package com.wuseguang.springcloud.controller;

import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.service.ClassRoomService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @描述 数据管理-->教室管理
 * @创建人 zm
 * @创建时间 2020/01/09
 */
@RestController
@RequestMapping("/classroom")
public class ClassRoomController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ClassRoomService classRoomService;

    //推送接口
    @Autowired
    private ClassCardService classCardService;

    /**
     * @描述 按条件查询 教室相关信息
     * @创建人 zm
     * @创建时间 2020/1/9
     */
    @RequestMapping("/queryClassRoomAll")
    public Result queryClassRoomAll(RequestParameterWrapper request,
                                    @RequestParam(value = "indexNo", required = false, defaultValue = "1") Integer indexNo,
                                    @RequestParam(value = "rows", required = false, defaultValue = "20") Integer rows,
                                    @RequestParam(value = "classid", required = false) String classid,//班级id
                                    @RequestParam(value = "classroomname", required = false) String classroomname) {//教室名称
        String school_id = request.getParam("schoolId");
        logger.info("school_id={},classroomname={},classid={},indexNo={}", school_id, classroomname, classid, indexNo);
        return classRoomService.queryClassRoomAll(indexNo, school_id, classid, classroomname, rows);
    }


    /**
     * @描述 删除教室  并删除和该教室绑定的设备
     * @创建人 zm
     * @创建时间 2020/1/9
     */
    @RequestMapping("/delClassRoom")
    public Result delClassRoom(RequestParameterWrapper request,
                               @RequestParam(value = "classroomid") Integer classroomid) {//教室id
        String school_id = request.getParam("schoolId");
        logger.info("school_id={},bancardids={}", school_id, classroomid);
        int i = classRoomService.delClassRoom(school_id, classroomid);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.DELETE_FAILED);
    }

    /**
     * @描述 修改教室信息
     * @创建人 zm
     * @创建时间 2020/1/9
     */
    @RequestMapping("/updateClassRoom")
    public Result updateClassRoom(RequestParameterWrapper request,
                                  @RequestParam(value = "classroomid") Integer classroomid,//教室id
                                  @RequestParam(value = "classid", required = false) String classid,//班级id
                                  @RequestParam(value = "classroomname", required = false) String classroomname,//教室名称
                                  @RequestParam(value = "remarks", required = false) String remarks) {//教室备注
        String school_id = request.getParam("schoolId");
        long l = classRoomService.querClassRoomCount(school_id, classroomname, classroomid);
        if (l > 0) {
            return new Result(CodeUtil.CAN_NOT_REPEAT);
        }
        if (!StringUtils.isEmpty(classid)) {
            long l1 = classRoomService.querClassRoomCountid(school_id, classid, classroomid);
            if (l1 > 0) {
                return new Result(CodeUtil.BIND_REPEATEDLY);
            }
        }
        logger.info("school_id={},bancardids={}", school_id, classroomid);
        int i = classRoomService.updateClassRoom(school_id, classroomid, classroomname, classid, remarks);
        if (i > 0) {
            classCardService.classesAndCards(classid);
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 班级置null
     * @创建人 zm
     * @创建时间 2020/2/23
     */
    @RequestMapping("/updateClassRoomNull")
    public Result updateClassRoom(RequestParameterWrapper request,
                                  @RequestParam(value = "classid") Integer classid) {//班级id
        String school_id = request.getParam("schoolId");
        classRoomService.updateClassRoom(school_id, classid);
        classCardService.classesAndCards(classid);
        return new Result();
    }


    /**
     * @描述 添加教室
     * @创建人 zm
     * @创建时间 2020/1/9
     */
    @RequestMapping("/addClassRoom")
    public Result addClassRoom(RequestParameterWrapper request,
                               @RequestParam(value = "classid", required = false) String classid,//班级id
                               @RequestParam(value = "classroomname", required = false) String classroomname,//教室名称
                               @RequestParam(value = "remarks", required = false) String remarks) {//教室备注
        String school_id = request.getParam("schoolId");
        logger.info("school_id={},classid={}", school_id, classid);
        long l = classRoomService.querClassRoomCount(school_id, classroomname, 0);
        if (l > 0) {
            return new Result(CodeUtil.CAN_NOT_REPEAT);
        }
        if (!StringUtils.isEmpty(classid)) {
            long l1 = classRoomService.querClassRoomCountid(school_id, classid, 0);
            if (l1 > 0) {
                return new Result(CodeUtil.BIND_REPEATEDLY);
            }
        }
        long i = classRoomService.addClassRoom(school_id, classroomname, classid, remarks);
        if (i > 0) {
            classCardService.classesAndCards(classid);
            return new Result();
        }
        return new Result(CodeUtil.ADD_FAILURE);
    }
}
